<template>
    <div>
      <div class="top-panel">
        <el-card>
          <el-form :model="searchForm" label-width="70px" label-position="right">
            <el-row>
              <el-col :span="5">
                <el-form-item label="书名">
                  <el-input
                    class="password-input"
                    v-model="searchForm.titleFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter="loadDataList"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="姓名">
                  <el-input
                    class="password-input"
                    v-model="searchForm.nameFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter="loadDataList"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="学工号">
                  <el-input
                    class="password-input"
                    v-model="searchForm.jobNumber"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter="loadDataList"
                  >
                  </el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4" :style="{ paddingLeft: '10px' }">
                <el-button type="success" @click="loadDataList()"
                  >查询
                </el-button>
                <el-button v-if="userInfo.admin||userInfo.superAdmin" type="danger" @click="showEdit()"
                  >新增借阅记录
                </el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
      <el-card class="table-data-card">
        <template #header>
          <span>借阅记录列表</span>
        </template>
        <Table
          ref="tableInfoRef"
          :columns="columns"
          :fetch="loadDataList"
          :dataSource="tableData"
          :options="tableOptions"
          :extHeight="tableOptions.extHeight"
        >
          <template #slotStatus="{ index, row }">
            <div class="row-op-panel">
              <Badge showType="red" text="未归还" v-if="row.status == 0"></Badge>
              <Badge showType="green" text="已归还" v-if="row.status == 1"></Badge>
            </div>
          </template>

          <template #roleTypeSlots="{ index, row }">
            <div class="row-op-panel">
              <Badge showType="green" text="学生" v-if="row.roleType == 0"></Badge>
              <Badge showType="blue" text="教师" v-if="row.roleType == 1"></Badge>
              <Badge showType="black" text="管理员" v-if="row.roleType == 2"></Badge>

            </div>
          </template>

          <template #slotOperation="{ index, row }">
            <div
              class="row-op-panel"

            >
              <a
                class="a-link"
                href="javascript:void(0)"
                @click="showEdit(row)"
                v-if="userInfo.admin&&row.status==0"
                >编辑</a
              >
              <a
                v-if="row.status==1"
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="goToReview(row)"
                >去评价</a
              >

              <a
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="updateStatus(row,1)"
                v-if="userInfo.admin&&row.status==0"

                >确认还书</a
              >
            </div>
          </template>
        </Table>
      </el-card>
      <BorrowRecordEdit ref="bookEditRef" @reload="loadDataList"></BorrowRecordEdit>

    </div>
  </template>

  <script setup>
  import BorrowRecordEdit from "./BorrowRecordEdit.vue";
  import { getCurrentInstance, nextTick, ref } from "vue";
import { useRouter, useRoute } from "vue-router";

  const { proxy } = getCurrentInstance();

  const userInfo = ref(proxy.VueCookies.get("userInfo") || {});
  const router = useRouter();
  const route = useRoute();
  const api = {
    loadDataList: "/borrowRecordInfo/loadDataList",
    getBorrowInfoById: "/borrowRecordInfo/getBorrowInfoById",
    saveBorrowRecord:"/borrowRecordInfo/saveBorrowRecord",
    updateStatus:"/borrowRecordInfo/updateStatus"
  };
  const searchForm = ref({});

  const tableData = ref({});
  const tableOptions = ref({
    extHeight: 125,
  });

  const columns = [
  {
      label: "编号",
      prop: "borrowId",
    },
    {
      label: "图书名称",
      prop: "title",
    },
    {
      label: "借阅人",
      prop: "name",
    },
    {
      label: "学工号",
      prop: "jobNumber",
    },


    {
      label: "借阅时间",
      prop: "borrowTime",
    },
    {
      label: "应还时间",
      prop: "dueTime",
    },
    {
      label: "归还时间",
      prop: "returnTime",
    },

    {
      label: "状态",
        scopedSlots:"slotStatus",
    },

    {
      label: "操作",
      prop: "operation",
      scopedSlots: "slotOperation",
      width:160
    },
  ];

  const tableInfoRef = ref();

  const loadDataList = async () => {
    let params = {
      pageNo: tableData.value.pageNo,
      pageSize: tableData.value.pageSize,
    };
    Object.assign(params, searchForm.value);
    let result = await proxy.Request({
      url: api.loadDataList,
      params: params,
    });
    if (!result) {
      return;
    }
    Object.assign(tableData.value, result.data);
  };

  //删除
  const deleteByBook = (data) => {
    proxy.Confirm(`确定要删除【${data.title}】吗？`, async () => {
      let result = await proxy.Request({
        url: api.deleteByBookId,
        params: {
            bookId: data.bookId,
        },
      });
      if (!result) {
        return;
      }
      proxy.Message.success("操作成功");
      loadDataList();
    });
  };

  //预约图书
  const updateStatus = (data,status) => {

    proxy.Confirm(`【${data.name}】确定已经【归还】【${data.title}】这本书吗？`, async () => {
      let result = await proxy.Request({
        url: api.updateStatus,
        params: {
          borrowId: data.borrowId,
          status:status,
        },
      });
      if (!result) {
        return;
      }
      proxy.Message.success("预约成功，请前往图书管理》图书预约查询记录，并查看预约须知");
      loadDataList();
    });
  };

  const bookEditRef = ref();
  const showEdit = (data = {}) => {
    bookEditRef.value.showEdit(Object.assign({}, data));
  };

  const goToReview=(data)=>{
    router.push(`/review/reviewDetail?bookId=${data.bookId}`);
  }

  </script>

  <style lang="scss" scoped>
  .detail-tree-panel {
    height: calc(100vh - 273px);
    overflow: auto;
    width: 100%;
  }
  </style>
